import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		addrlist:{
			name:'',
			tel:'',
			addr:'',
		},
		detail:{
			title:'',
			price:'',
			image:'',
			_id:'',
			num:''
		},
		hasLogin: false,
		isUniverifyLogin: false,
		loginProvider: "",
		openid: null,
		testvuex: false,
		noMatchLeftWindow: true,
		active: 'componentPage',
		leftWinActive: '/pages/component/view/view',
		activeOpen: '',
		univerifyErrorMsg: '',
		topList:[{
			title:'分期Apple/苹果 iPhone 12 5G国行官方正品12promax原封手机 紫色',
			img:'/static/image/f4.png',
			bp:'分期付款',
			id:'01',
			price:'4589.0'
		},
		{
			title:'芭芭鸭儿童板鞋女童鞋子男童小白鞋白色春季2021年春款新款夏单鞋',
			img:'/static/image/f5.png',
			bp:'退货包运费',
			id:'02',
			price:'188.8'
		},
		{
			title:'双人床现代简约主卧储物床1.5米轻奢板式床1.8北欧经济高箱收纳床',
			img:'/static/image/f3.png',
			bp:'上门安转',
			id:'03',
			price:'2656.9'
		},
		{
			title:'Oece气质小西装2021年春装新款女装复古英伦炸街韩版休闲西服外套',
			img:'/static/image/f1.png',
			bp:'退货包运费',
			id:'04',
			price:'456.7'
		},
		{
			title:'松鼠铺子坚果零食礼包年货礼盒混合特产小吃节日团购送人干果',
			img:'/static/image/f7.png',
			bp:'极速退款',
			id:'05',
			price:'88.5'
		},
		{
			title:'大头围海边度假草帽女可折叠防晒大檐夏天遮阳帽沙滩帽子百搭韩版',
			img:'/static/image/f2.png',
			bp:'极速发货',
			id:'06',
			price:'18.8'
		},
		
		]
	},
	mutations: {
		getAddress(state, value){
			state.addrlist.name = value.name
			state.addrlist.tel = value.tel 
			state.addrlist.addr = value.addr
		},
		getDetail(state, value){
			state.detail.title = value.title
			state.detail.price= value.price 
			state.detail.image = value.image
			state.detail._id = value._id
			state.detail.num = value.num
		},
		login(state, provider) {
			state.hasLogin = true;
			state.loginProvider = provider;
		},
		logout(state) {
			state.hasLogin = false
			state.openid = null
		},
		setOpenid(state, openid) {
			state.openid = openid
		},
		setTestTrue(state) {
			state.testvuex = true
		},
		setTestFalse(state) {
			state.testvuex = false
		},
		setMatchLeftWindow(state, matchLeftWindow) {
			state.noMatchLeftWindow = !matchLeftWindow
		},
		setActive(state, tabPage) {
			state.active = tabPage
		},
		setLeftWinActive(state, leftWinActive) {
			state.leftWinActive = leftWinActive
		},
		setActiveOpen(state, activeOpen) {
			state.activeOpen = activeOpen
		},
		setUniverifyLogin(state, payload) {
			typeof payload !== 'boolean' ? payload = !!payload : '';
			state.isUniverifyLogin = payload;
		},
		setUniverifyErrorMsg(state,payload = ''){
			state.univerifyErrorMsg = payload
		}
	},
	getters: {
		
	},
	actions: {
		// lazy loading openid
		getUserOpenId: async function({
			commit,
			state
		}) {
			return await new Promise((resolve, reject) => {
				if (state.openid) {
					resolve(state.openid)
				} else {
					uni.login({
						success: (data) => {
							commit('login')
							setTimeout(function() { //模拟异步请求服务器获取 openid
								const openid = '123456789'
								console.log('uni.request mock openid[' + openid + ']');
								commit('setOpenid', openid)
								resolve(openid)
							}, 1000)
						},
						fail: (err) => {
							console.log('uni.login 接口调用失败，将无法正常使用开放接口等服务', err)
							reject(err)
						}
					})
				}
			})
		},
		
		
	}
})

export default store
